<?php

class LessionController extends BaseController {

    public function __construct($action, $urlValue) {
        if (!Helper::isLogin()) {
            $_SESSION['lastUrl'] = Helper::currentPageURL();
            header('Location: ' . BASE_URL . 'account/login');
        }
        parent::__construct($action, $urlValue);
        require("models/lession.php");
        $this->model = new LessionModel();
    }

    public function index() {
        $this->view->output($this->model->index());
    }

    protected function create() {
        if (!empty($_POST)) {

            if (isset($_POST["lession_name"])) {
                $result = $this->model->create($_POST);
                if ($result) {
                    $arlet = array(
                        'class' => 'alert-success',
                        'message' => 'Add lession success'
                    );
                    $_SESSION['message'] = $arlet;
                    header('Location: ' . BASE_URL . 'lession/index');
                }
            } else {
                $alert = array(
                    'class' => 'arlet-error',
                    'message' => 'Fill all field'
                );
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'lession/create');
            }
        }
        $this->view->output($this->model->create(""));
    }

    protected function update() {
        if (!empty($_POST)) {

            $id = $_POST['lession_id'];
            foreach ($_POST as $col => $value) {
                $data[$col] = $value;
            }
            unset($data['lession_id']);
            //update item -> return total rows affected
            $result = $this->model->edit($id, $data);
            echo $result;
            if ($result) {
                echo "quangthien";
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Add lession success'
                );
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'lession/index');
            }
        }
        //load form update
        $this->view->output($this->model->edit($id = $this->urlValues['id'], $data = ''));
    }
    public function getlist(){
        $this->view->output($this->model->getList($this->urlValues['id']),"empty");
    }

    protected function delete() {
        if (!empty($_POST)) {
            $result = $this->model->delete('lession', 'lession_id IN(' . implode(',', $_POST['id']) . ')');
        } else {
            $result = $this->model->delete('lession', 'lession_id=' . $this->urlValues['id']);
        }
        if (result) {
            $arlet = array(
                'class' => 'arlet-success',
                'message' => 'Add lession success'
            );
            $_SESSION['message'] = $arlet;
            header('Location: ' . BASE_URL . 'lession/index');
        }
    }
    
}
